<template>
  <div class="main">
    <div class="title">
      <a class="active" style="text-decoration:none;" href="/login">登录</a>
      <span>·</span>
      <a style="text-decoration:none;" href="/register">注册</a>
    </div>

    <div class="sign-up-container">
      <el-form ref="userForm" :model="user">
        <el-form-item
          class="input-prepend restyle"
          prop="mail"
          :rules="[
            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
            {
              type: 'email',
              message: '请输入正确的邮箱地址',
              trigger: ['blur', 'change']
            }
          ]"
        >
          <div>
            <el-input type="text" placeholder="邮箱号" v-model="user.mail" />
            <i class="iconfont icon-user" />
          </div>
        </el-form-item>

        <el-form-item
          class="input-prepend"
          prop="password"
          :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]"
        >
          <div>
            <el-input
              type="password"
              placeholder="密码"
              v-model="user.password"
            />
            <i class="iconfont icon-password" />
          </div>
        </el-form-item>

        <div class="btn">
          <input
            type="button"
            class="sign-in-button"
            value="登录"
            v-loading.fullscreen.lock="fullscreenLoading"
            @click="submitLogin()"
          />
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import "~/assets/css/sign.css";
import "~/assets/css/iconfont.css";
import cookie from "js-cookie";
import loginApi from "@/api/login";
export default {
  layout: "sign",

  data() {
    return {
      user: {
        mail: "",
        password: ""
      },
      loginInfo: {},
      Ticket: "",
      Randstr: "",
      fullscreenLoading: false
    };
  },

  methods: {
    submitLogin() {
      this.fullscreenLoading = true;
      loginApi.submitLogin(this.user).then(response => {
        if (response) {
          //把token存在cookie中、也可以放在localStorage中
          cookie.set("fuyao_token", response.data.data.token, {
            domain: "localhost"
          });

          //登录成功根据token获取用户信息
          loginApi.getLoginInfo().then(response => {
            console.log("code:" + response.data.code);
            //将用户信息记录cookie
            cookie.set(
              "fuyao_ucenter",
              JSON.stringify(response.data.data.item),
              {
                domain: "localhost"
              }
            );
            //跳转页面
            window.location.href = "/";
          });
        }
      });
    }
  }
};
</script>
<style>
.el-form-item__error {
  z-index: 9999999;
}
</style>
